package com.zhudao.crawler.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class DownloadImgDao {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;//只读


    public List<Map<String, Object>> getList(String imageType, Integer startIndex) {
        return jdbcTemplate.queryForList("SELECT * FROM flat_room_picture WHERE STATUS = 1 AND verifyStatus IN (1, 2) AND imageType =? ORDER BY id DESC LIMIT ?,3000", imageType, startIndex);
    }

    public boolean getByPictureId(Integer pictureId) {
        Map<String, Object> map = jdbcTemplate.queryForMap("SELECT COUNT(1) as count from flat_room_aliyun_picture where  pictureId=?", pictureId);
        Integer count = Integer.valueOf(map.get("count").toString());
        return count > 0 ? true : false;
    }

    @Transactional
    public void saveAliyunPicture(Integer pictureId, String imagePath) {
        jdbcTemplate.update("INSERT into flat_room_aliyun_picture" +
                "        (id,pictureId,imagePath,createTime,valid)" +
                " VALUES (NULL,?,?,?,?)", pictureId, imagePath, new Date(), 1);
    }
}
